package com.sophic_tech.hkb.service.user.Ipml;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.sophic_tech.hkb.entity.Result;
import com.sophic_tech.hkb.entity.UserModel;
import com.sophic_tech.hkb.mapper.UserMapper;
import com.sophic_tech.hkb.service.user.GetUserInfoService;
import com.sophic_tech.hkb.service.user.UpdateUsernameService;
import com.sophic_tech.hkb.utils.JWTUtils;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.security.SignatureException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserService implements GetUserInfoService, UpdateUsernameService {
    @Resource
    private UserMapper userMapper;
    @Override
    public Result getUserInfo(String UserId, String token) {
        log.info("开始获取用户信息，请求UserId: {}", UserId);
        if (token == null) {
            log.warn("获取用户信息失败：Token为空，UserId: {}", UserId);
            return Result.error(401, "未登录");
        }
        try {
            DecodedJWT decodedjwt = JWTUtils.verify(token);
            int userId = decodedjwt.getClaim("id").asInt();
            log.debug("Token验证成功，Token中的userId: {}, 请求的UserId: {}", userId, UserId);
            if (userId != Integer.parseInt(UserId)) {
                log.warn("权限校验失败：Token用户ID({})与请求ID({})不匹配", userId, UserId);
                return Result.error(403, "权限不足");
            }
            log.info("查询用户信息，userId: {}", userId);
            UserModel userModel = userMapper.selectById(userId);
            if (userModel == null) {
                log.warn("未找到用户信息，userId: {}", userId);
                return Result.error(404, "用户不存在");
            }
            log.info("成功获取用户信息，userId: {}", userId);
            return Result.success("获取用户信息成功", userModel);
        } catch (ExpiredJwtException e) {
            log.error("Token已过期，UserId: {}, 错误信息: {}", UserId, e.getMessage());
            return Result.error(401, "token已过期，请重新登录");
        } catch (SignatureException e) {
            log.error("Token签名验证失败，UserId: {}, 错误信息: {}", UserId, e.getMessage());
            return Result.error(401, "token签名错误，请重新登录");
        }catch (Exception e) {
            log.error("获取用户信息时发生未知异常，UserId: {}", UserId, e);
            return Result.error(500, "获取用户信息失败");
        }
    }
    @Override
    public Result updateUserName(String UserId, String token, String newUserName) {
        log.info("开始更新用户名，UserId: {}, 新用户名: {}", UserId, newUserName);
        if (token == null) {
            log.warn("更新用户名失败：Token为空，UserId: {}", UserId);
            return Result.error(401, "未登录");
        }
        try {
            DecodedJWT decodedjwt = JWTUtils.verify(token);
            int userId = decodedjwt.getClaim("id").asInt();
            log.debug("Token验证成功，Token中的userId: {}, 请求的UserId: {}", userId, UserId);
            if (userId != Integer.parseInt(UserId)) {
                log.warn("权限校验失败：Token用户ID({})与请求ID({})不匹配", userId, UserId);
                return Result.error(403, "权限不足");
            }
            log.info("更新用户名，userId: {}, 新用户名: {}", userId, newUserName);
            UserModel userModel = userMapper.selectById(userId);
            if (userModel == null) {
                log.warn("未找到用户信息，userId: {}", userId);
                return Result.error(404, "用户不存在");
            }
            userModel.setUsername(newUserName);
            userMapper.updateById(userModel);
            log.info("成功更新用户名，userId: {}, 新用户名: {}", userId, newUserName);
            return Result.success("更新用户名成功", userModel);
        }catch (ExpiredJwtException e) {
            log.error("Token已过期，UserId: {}, 错误信息: {}", UserId, e.getMessage());
            return Result.error(401, "token已过期，请重新登录");
        } catch (SignatureException e) {
            log.error("Token签名验证失败，UserId: {}, 错误信息: {}", UserId, e.getMessage());
            return Result.error(401, "token签名错误，请重新登录");
        }catch (Exception e) {
            log.error("获取用户信息时发生未知异常，UserId: {}", UserId, e);
            return Result.error(500, "获取用户信息失败");
        }
    }
}
